package com.android.monee.view.model;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;

import com.android.monee.data.model.Mensuality;
//import com.android.monee.data.model.Mensuality.MensualityState;
import com.android.monee.domain.MensGrp;
import com.android.monee.R;

public class MensGrpHistoFollow extends MensGrp {

	private SimpleDateFormat sdf;
	private String currency;
	private Resources res;
	private Float deltaOptmz;
		
	public MensGrpHistoFollow(MensGrp mensGroup, Resources res) {
		super(mensGroup.getDate(), mensGroup);
		constructor(res);
	}
	
	private void constructor(Resources res){
		this.sdf = new SimpleDateFormat("MMMM yyyy");
		this.currency = res.getText(R.string.currency).toString();
		this.res = res;
		this.deltaOptmz = 0f;
	}

	public String getDeltaLib() {
		if(this.deltaOptmz > 0)
			return "+"+this.deltaOptmz;
		else if(this.deltaOptmz == 0)
			return "";
		else
			return this.deltaOptmz.toString();
	}

	public void setOptmzDelta(Float delta) {
		this.deltaOptmz = delta;
	}
	
	public int getDeltaColor(){
		if(this.deltaOptmz > 0)
			return Color.parseColor("#b70000");
		else if(this.deltaOptmz == 0)
			return Color.TRANSPARENT;
		else
			return Color.parseColor("#009901");		
	}
	
	public String getDateLib(){
		String dateLib = sdf.format(super.getDate()); 
		return dateLib.toUpperCase().substring(0, 1)+dateLib.substring(1);
	}
	
	public String getAmountLib(){
		if(super.areAllDone() || super.areAnyDone()){
			DecimalFormat format = new DecimalFormat("#.#"+this.currency);
			return format.format(super.getTotalAmount());
		}
		else {
			DecimalFormat provFormat = new DecimalFormat("#.#/");
			DecimalFormat totalFormat = new DecimalFormat("#.#"+this.currency);
			return provFormat.format(super.getProvAmount())+totalFormat.format(super.getTotalAmount());
		}
	}
		
	public String getNotProvAmountLib(){
		DecimalFormat format = new DecimalFormat("#.#"+this.currency);
		return format.format(super.getNotProvAmount());
	}
	
	public boolean getStrike(){
		return areAllDone();
	}
	
	public Drawable getBackground(){
		if(areAllDone())
			return res.getDrawable(R.drawable.histo_follow_mens_group_dark);
		else
			return res.getDrawable(R.drawable.histo_follow_mens_group_light);
	}
		
	public int getTextColor(){
		if(areAllDone()){
			return Color.LTGRAY;
		}
		else
			return Color.BLACK;
	}
	
	public MensualityHistoFollow get(int location) {
		return new MensualityHistoFollow(super.get(location), this.res);
	}
	
	public static List<MensGrpHistoFollow> newCollection(Collection<? extends MensGrp> groups, Resources res){
		List<MensGrpHistoFollow> newCol = new ArrayList<MensGrpHistoFollow>();
		for(MensGrp mensGroup : groups){
			newCol.add(new MensGrpHistoFollow(mensGroup, res));
		}
		return newCol;
	}

	public static List<MensGrpHistoFollow> newCollection(
			List<MensGrp> mensGrps, Map<MensGrp, Float> optmzStats,
			Resources resources) {
		List<MensGrpHistoFollow> newCol = newCollection(mensGrps, resources);
		
		for(MensGrp mensGrp : optmzStats.keySet()){
			int i = Collections.binarySearch(newCol, mensGrp);
			
			if(i >= 0){
				newCol.get(i).setOptmzDelta(optmzStats.get(mensGrp));
			}
		}
		return newCol;
		
	}
}
